#include "app.h"
#include "ble.h"
#include "led.h"
#include "common.h"

TaskHandle_t star_handle;
TaskHandle_t ble_handle;
TaskHandle_t blink_handle;

void star_app(void *p);
void app_task(void *p);
void bilink_task(void *p);

void app_init()
{
    for (size_t i = 0; i < 4; i++)
    {
        pinMode(pin_arr[i], OUTPUT);
        digitalWrite(pin_arr[i], LOW);
    }
    Serial.println("pin star");
    ble_init();
    LED_init();
    xTaskCreate(star_app, "star", 9216, NULL, 3, &star_handle);
}

void star_app(void *p)
{

    xTaskCreate(app_task, "app_task", 9216, NULL, 4, &ble_handle);
    xTaskCreate(bilink_task, "blinke_task", 9216, NULL, 4, &blink_handle);
    vTaskDelay(pdMS_TO_TICKS(100));
    vTaskDelete(NULL);
}
void bilink_task(void *p)
{
    for (;;)
    {
        LED_run();


        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

void app_task(void *p)
{
    for (;;)
    {
        if (is_ble_connected)
        {
            for (size_t i = 0; i < 4; i++)
            {
                notify_state_change(i + 1, digitalRead(pin_arr[i]));
                inspect_pin_state(i + 1, digitalRead(pin_arr[i]));
            }
        }
        vTaskDelay(pdMS_TO_TICKS(30000)); // 每30秒检查一次
    }
}
